#ifndef __CONF_YAML_H__
#define __CONF_YAML_H__

#include <inttypes.h>
#include <stdbool.h>

#include "queue.h"

/**
 * yaml配置文件解析结构
 */
typedef struct ConfNode_ {
    char *name; // 指针，赋值需要申请空间
    char *val;  // 赋值需要申请空间

    int is_seq;   // 标记此节点是列表的一个节点

    int final;    // 最终标记，设置为 final 之后，值不能被覆盖

    struct ConfNode_ *parent;         // 父节点
    TAILQ_HEAD(, ConfNode_) head;     // 孩子节点们
    TAILQ_ENTRY(ConfNode_) next;      // 兄弟节点们
} ConfNode;


/**
 * 默认日志地址
 */
#define DEFAULT_LOG_DIR "/var/log/yaml.log"
#define DEFAULT_DATA_DIR DATA_DIR

#define unlikely(expr) __builtin_expect(!!(expr), 0)

void ConfInit(void);
void ConfDeInit(void);
ConfNode *ConfGetRootNode(void);
int ConfGet(const char *name, const char **vptr);
int ConfGetInt(const char *name, intmax_t *val);
int ConfGetBool(const char *name, int *val);
int ConfGetDouble(const char *name, double *val);
int ConfGetFloat(const char *name, float *val);
int ConfSet(const char *name, const char *val);
int ConfSetFromString(const char *input, int final);
int ConfSetFinal(const char *name, const char *val);
void ConfDump(void);
void ConfNodeDump(const ConfNode *node, const char *prefix);
ConfNode *ConfNodeNew(void);
void ConfNodeFree(ConfNode *);
ConfNode *ConfGetNode(const char *key);
void ConfCreateContextBackup(void);
void ConfRestoreContextBackup(void);
ConfNode *ConfNodeLookupChild(const ConfNode *node, const char *key);
const char *ConfNodeLookupChildValue(const ConfNode *node, const char *key);
void ConfNodeRemove(ConfNode *);
void ConfRegisterTests(void);
int ConfNodeChildValueIsTrue(const ConfNode *node, const char *key);
int ConfValIsTrue(const char *val);
int ConfValIsFalse(const char *val);
void ConfNodePrune(ConfNode *node);
int ConfRemove(const char *name);
bool ConfNodeHasChildren(const ConfNode *node);

ConfNode *ConfGetChildWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name);
ConfNode *ConfNodeLookupKeyValue(const ConfNode *base, const char *key, const char *value);
int ConfGetChildValue(const ConfNode *base, const char *name, const char **vptr);
int ConfGetChildValueInt(const ConfNode *base, const char *name, intmax_t *val);
int ConfGetChildValueBool(const ConfNode *base, const char *name, int *val);
int ConfGetChildValueWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name, const char **vptr);
int ConfGetChildValueIntWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name, intmax_t *val);
int ConfGetChildValueBoolWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name, int *val);
char *ConfLoadCompleteIncludePath(const char *);
int ConfNodeIsSequence(const ConfNode *node);
ConfNode *ConfSetIfaceNode(const char *ifaces_node_name, const char *iface);
int ConfSetRootAndDefaultNodes(
        const char *ifaces_node_name, const char *iface, ConfNode **if_root, ConfNode **if_default);
ConfNode *ConfNodeGetNodeOrCreate(ConfNode *parent, const char *name, int final);

int PathIsAbsolute(const char *path) ;
int PathIsRelative(const char *path) ;
int ConfGetList(const char *name, const char *(**val)[]) ;

#endif /* !__CONF_YAML_H__ */
